
import healththink.*
//import grails.plugins.springsecurity.*
import grails.util.Environment

class BootStrap {
    def springSecurityService
    
    def init = { servletContext ->

        switch (Environment.current) {

            case Environment.DEVELOPMENT:
                def userOwner = User.findByEmail("owner@test.com") ?: 
                    new User(
                        username:"owner", 
                        firstName: "TestFName",
                        lastName: "testLName",
                        //userId:"owner1",
                        email:"owner@test.com",
                        //password:springSecurityService.encodePassword("password"),
                        password:"password",
                        enabled:true
                    ).save(flush:true)
                def userModerator = User.findByEmail("moderator@test.com") ?: 
                    new User(
                        username:"moderator", 
                        firstName: "TestFName",
                        lastName: "testLName",
                        //userId:"moderator1",
                        email:"moderator@test.com",
                        //password:springSecurityService.encodePassword("password"),
                        password:"password",
                        enabled:true
                    ).save(flush:true)
                def userContributor = User.findByEmail("contributor@test.com") ?: 
                    new User(
                        username:"contributor", 
                        firstName: "TestFName",
                        lastName: "testLName",
                        //userId:"contributor1",
                        email:"contributor@test.com",
                        //password:springSecurityService.encodePassword("password"),
                        password:"password",
                        enabled:true
                    ).save(flush:true)

                String Owner = "ROLE_" + UserType.Owner.toString().toUpperCase()
                String Contributor = "ROLE_" + UserType.Contributor.toString().toUpperCase()
                String Moderator = "ROLE_" + UserType.Moderator.toString().toUpperCase()

                def ownerRole = SecRole.findByAuthority(Owner) ?: 
                    new SecRole(authority: Owner).save()
                def contributorRole = SecRole.findByAuthority(Contributor) ?: 
                    new SecRole(authority: Contributor).save()
                def moderatorRole = SecRole.findByAuthority(Moderator) ?: 
                    new SecRole(authority: Moderator).save()

                SecUserSecRole.create userOwner, ownerRole
                SecUserSecRole.create userContributor, contributorRole
                SecUserSecRole.create userModerator, moderatorRole
                break;

            case Environment.PRODUCTION:
             def userOwner = User.findByEmail("owner@test.com") ?: 
                    new User(
                        username:"owner", 
                        firstName: "TestFName",
                        lastName: "testLName",
                        //userId:"owner1",
                        email:"owner@test.com",
                        //password:springSecurityService.encodePassword("password"),
                        password:"password",
                        enabled:true
                    ).save(flush:true)
                def userModerator = User.findByEmail("moderator@test.com") ?: 
                    new User(
                        username:"moderator", 
                        firstName: "TestFName",
                        lastName: "testLName",
                        //userId:"moderator1",
                        email:"moderator@test.com",
                        //password:springSecurityService.encodePassword("password"),
                        password:"password",
                        enabled:true
                    ).save(flush:true)
                def userContributor = User.findByEmail("contributor@test.com") ?: 
                    new User(
                        username:"contributor", 
                        firstName: "TestFName",
                        lastName: "testLName",
                        //userId:"contributor1",
                        email:"contributor@test.com",
                        //password:springSecurityService.encodePassword("password"),
                        password:"password",
                        enabled:true
                    ).save(flush:true)

                String Owner = "ROLE_" + UserType.Owner.toString().toUpperCase()
                String Contributor = "ROLE_" + UserType.Contributor.toString().toUpperCase()
                String Moderator = "ROLE_" + UserType.Moderator.toString().toUpperCase()

                def ownerRole = SecRole.findByAuthority(Owner) ?: 
                    new SecRole(authority: Owner).save()
                def contributorRole = SecRole.findByAuthority(Contributor) ?: 
                    new SecRole(authority: Contributor).save()
                def moderatorRole = SecRole.findByAuthority(Moderator) ?: 
                    new SecRole(authority: Moderator).save()

                SecUserSecRole.create userOwner, ownerRole
                SecUserSecRole.create userContributor, contributorRole
                SecUserSecRole.create userModerator, moderatorRole
                break;
        }   
    }

    def destroy = {
    }

}
